//index.js
//获取应用实例
const app = getApp()
const db = wx.cloud.database()
const _ = db.command

Page({
  data: {
    HomeIndex: 0,
    concerts: [],
    concerts_yanchanghui: [],
    concerts_yinyuehui: [],
    concerts_huajugeju: [],
    concerts_wudaobalei: [],
    navList: [{
        "title": "演唱会",
        "src": "/images/hot/3.png"
      },
      {
        "title": "音乐会",
        "src": "/images/hot/4.png"
      },
      {
        "title": "话剧歌剧",
        "src": "/images/hot/5.png"
      },
      {
        "title": "舞蹈芭蕾",
        "src": "/images/hot/6.png"
      }
    ],
  },

getNavList:function(){
  wx.request({
    url:"",
    success(res){
      console.log(res.data);
    }
  })
},
// onTap:function(item){
//   wx.navigateTo({
//     url: '../detail/detail',
//   })
// },
  boxtwo: function (e) {
    var index = parseInt(e.currentTarget.dataset.index)
    this.setData({
      HomeIndex: index
    })
  },


  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  onLoad: function() {
    // if(app.globalData.openid){
    //   this.openid = app.globalData.openid
    // }else{
    //   app.openIDReadyCallback = openid => {
    //     this.openid = openid
    //   }
    // }
    wx.cloud.init()
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        this.openid = res.result.openid
        db.collection('verifiedperson').where({
          _userid: this.openid // 填入当前用户 openid
        }).get({
          success: res => {
            if (Object.keys(res.data).length == 0) {
              wx.showModal({
                title: '提示',
                content: '领票前需要录入人脸，请点击确认录入',
                success: function (res) {
                  console.log(res)
                  if (res.confirm) {
                    wx.navigateTo({
                      url: '../entry/entry',
                    })
                  } else {
                    wx.showToast({
                      title: '需要录入人脸可以在"我的"页面中录入人脸',
                      icon: 'none',
                      duration: 2000
                    })
                  }
                }
              })
            }
          },
          fail: err => {
            console.log(err)
          }
        })
      },
      fail: err => {
        console.error(err)
      }
    })

    db.collection('concert').get({
      success: res => {
        console.log(res.data)
        this.setData({
          concerts: res.data
        }) 
        //对读取的内容进行分类
        var temp_yanchanghui = [];
        var temp_yinyuehui = [];
        var temp_huajugeju = [];
        var temp_wudaobalei = [];
        res.data.forEach(function(item, index) {
          if (item.type == "演唱会") temp_yanchanghui.push(item)
          if (item.type == "音乐会") temp_yinyuehui.push(item)
          if (item.type == "话剧歌剧") temp_huajugeju.push(item)
          if (item.type == "舞蹈芭蕾") temp_wudaobalei.push(item)
        });
        this.setData({
          concerts_yanchanghui: temp_yanchanghui,
          concerts_yinyuehui: temp_yinyuehui,
          concerts_huajugeju: temp_huajugeju,
          concerts_wudaobalei: temp_wudaobalei
        })
      }
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
      
    }
  }
  

})